package com.sskj.mine.ui.activity;


import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.PersonCenterItemBean;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.PersonCenterActivityPresenter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.mine.util.HideUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = RConfig.MINE_PERSON_CENTER)//个人中心
public class PersonCenterActivity extends BaseActivity<PersonCenterActivityPresenter> {
    @Inject
    UserViewModel userViewModel;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    private UserData userData;
    private SlimAdapter<PersonCenterItemBean> slimAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_person_center;
    }

    @Override
    public PersonCenterActivityPresenter getPresenter() {
        return new PersonCenterActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        setTitle(App.INSTANCE.getString(R.string.minePersonCenterActivity0));
        initRecy();
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            }
            updateUI();
        });
    }

    private void initRecy() {
        ArrayList<PersonCenterItemBean> itemBeans = new ArrayList<>();
        itemBeans.add(new PersonCenterItemBean(App.INSTANCE.getString(R.string.minePersonCenterActivity1), ""));
        itemBeans.add(new PersonCenterItemBean(App.INSTANCE.getString(R.string.minePersonCenterActivity2), ""));
        itemBeans.add(new PersonCenterItemBean(App.INSTANCE.getString(R.string.minePersonCenterActivity5), ""));
        itemBeans.add(new PersonCenterItemBean(App.INSTANCE.getString(R.string.minePersonCenterActivity7), ""));
        itemBeans.add(new PersonCenterItemBean(App.INSTANCE.getString(R.string.minePersonCenterActivity6), ""));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 2)));
        slimAdapter = SlimAdapter.create().register(R.layout.mine_recy_item_person_center, new SlimInjector<PersonCenterItemBean>() {
            @Override
            public void onInject(PersonCenterItemBean data, IViewInjector injector, List payloads) {
                injector
                        .text(R.id.tvTitle, data.getTitle())
                        .text(R.id.tvContent, data.getContent());
            }
        }).attachTo(recyclerView).updateData(itemBeans);
    }

    /*
    * 0 手机号
    * 1 邮箱地址
    * 2 姓名
    * 3 身份证号
    * 4 护照号码
    * */
    private void updateUI() {
        if (userData != null) {
            slimAdapter.getData().get(0).setContent(HideUtil.getPhoneHide(userData.getMobile()));
            slimAdapter.getData().get(1).setContent(userData.getMail());
            if (userData.getStatus() == 3) {
                slimAdapter.getData().get(2).setContent(TextUtils.isEmpty(userData.getRealname()) ? App.INSTANCE.getString(R.string.mine_un_certification) : userData.getRealname());
                if (userData.getAuth_type() == 1) {
                    slimAdapter.getData().get(3).setContent(TextUtils.isEmpty(userData.getIdcard()) ? App.INSTANCE.getString(R.string.mine_un_certification) : HideUtil.getIDCardHide(userData.getIdcard()));
                    slimAdapter.getData().get(4).setContent("--");
                } else {
                    slimAdapter.getData().get(4).setContent(TextUtils.isEmpty(userData.getIdcard()) ? App.INSTANCE.getString(R.string.mine_un_certification) : HideUtil.getIDCardHide(userData.getIdcard()));
                    slimAdapter.getData().get(3).setContent("--");
                }
            } else {
                slimAdapter.getData().get(2).setContent("--");
                slimAdapter.getData().get(3).setContent("--");
                slimAdapter.getData().get(4).setContent("--");
            }
            slimAdapter.notifyDataSetChanged();
        }
    }


}
